package com.zjl.Kafka.第03章_操作kafka;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.HashMap;

/**
 * 我们的常规数据主要分为两部分: 批次头+数据体
 *  例子: 这是 sh kafka-run-class.sh kafka.tools.DumpLogSegments --files 000.log --print-data-log  的查看案例 (不同版本也许不一致)
 * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 * baseOffset: 0 lastOffset: 0 count: 1 baseSequence: 0 lastSequence: 0 producerId: 7020 producerEpoch: 0 partitionLeaderEpoch: 0 isTransactional: false isControl: false deleteHorizonMs: OptionalLong.empty position: 0 CreateTime: 1725717285031 size: 78 magic: 2 compresscodec: none crc: 1303487529 isvalid: true
 * | offset: 0 CreateTime: 1725717285031 keySize: 4 valueSize: 6 sequence: 0 headerKeys: [] key: key0 payload: value0
 * baseOffset: 1 lastOffset: 1 count: 1 baseSequence: 1 lastSequence: 1 producerId: 7020 producerEpoch: 0 partitionLeaderEpoch: 0 isTransactional: false isControl: false deleteHorizonMs: OptionalLong.empty position: 78 CreateTime: 1725717285160 size: 78 magic: 2 compresscodec: none crc: 369833706 isvalid: true
 * | offset: 1 CreateTime: 1725717285160 keySize: 4 valueSize: 6 sequence: 1 headerKeys: [] key: key1 payload: value1
 * baseOffset: 2 lastOffset: 2 count: 1 baseSequence: 2 lastSequence: 2 producerId: 7020 producerEpoch: 0 partitionLeaderEpoch: 0 isTransactional: false isControl: false deleteHorizonMs: OptionalLong.empty position: 156 CreateTime: 1725717285197 size: 78 magic: 2 compresscodec: none crc: 1046726886 isvalid: true
 * | offset: 2 CreateTime: 1725717285197 keySize: 4 valueSize: 6 sequence: 2 headerKeys: [] key: key2 payload: value2
 * .....
 * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 * 批次头：
 * BASE OFFSET                        8     基础偏移量偏移量
 * LENGTH OFFSET                      4     长度偏移量
 * PARTITION LEADER EPOCH OFFSET      4     Leaader分区纪元偏移量
 * MAGIC OFFSET                       1     魔数偏移量
 * ATTRIBUTES OFFSET                  2     属性偏移量
 * BASE TIMESTAMP OFFSET              8     基础时间戳偏移量
 * MAX TIMESTAMP OFFSET               8     最大时间戳偏移量
 * LAST OFFSET DELTA OFFSET           4     最后偏移量偏移量
 * PRODUCER ID OFFSET                 8     生产者ID偏移量
 * PRODUCER EPOCH OFFSET              2     生产者纪元偏移量
 * BASE SEQUENCE OFFSET               4     基础序列号偏移量
 * RECORDS COUNT OFFSET               4     记录数量偏移量
 * CRC OFFSET                         4     CRC校验偏移量
 *
 * 总字节数 61 byte
 * *****************************************************************
 * 数据体
 * size                 固定值                         1
 * offsetDelta          固定值                         1
 * timestampDelta       时间戳                         1
 * keySize             Key字节长度                     1 (动态)
 * keySize(Varint)    Key变量压缩长度算法需要大小         1 (动态)
 * valueSize            value字节长度                  1 (动态)
 * valueSize(Varint)    Value变量压缩长度算法需要大小     1 (动态)
 * Headers              数组固定长度                    1 (动态)
 * sizeInBytes       上面长度之和的压缩长度算法需要大小      1
 *
 *
 *
 *
 */
public class D2_数据的字节存储 {
    public static void main(String[] args) {
        //每一批大小  单位kb  用于存储数据时对每批的大小限制   大小包含 批次头 + 数据体  当大于此值时，就会新建一批
        new HashMap<>().put(ProducerConfig.BATCH_SIZE_CONFIG, 100);
        //比如 以下案例怎样计算
        ProducerRecord<String,String> record = new ProducerRecord<>(
                "x03"//主题名称  发送不成功改动主题名，就行了
                ,"key"//数据的key     占3字节
                ,"value"//数据的value  占5字节
        );
        // 61 头 +  4 为体固定 + 8 位值的大小 + 1位Key字节长度  + 1位value字节长度  +  1位数组固定长度 = 76
        System.out.println(61 + 4 + 8 + 1 + 1 + 1);
    }

}
